﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Security;
using System;

namespace DocTes.Users
{
    /// <summary>
    /// Clase Data source de objetos de usuario usada para manejar el gridview de usuarios
    /// </summary>
    [DataObject(true)]
    public class DataSourceMembershipUser
    {
        /// <summary>
        /// Método de borrar usuario
        /// </summary>
        /// <param name="UserName"></param>
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        static public void Borrar(string UserName)
        {
            Membership.DeleteUser(UserName, true);
        }

        /// <summary>
        /// Método de selección de usuarios
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        static public List<MembershipUserWrapper> ListadoUsuarios()
        {

            List<MembershipUserWrapper> listadoUsuarios = new List<MembershipUserWrapper>();


            MembershipUserCollection usuarioColeccion = Membership.GetAllUsers();
            foreach (MembershipUser usuario in usuarioColeccion)
            {
                MembershipUserWrapper usuarioWrapper = new MembershipUserWrapper(usuario);
                listadoUsuarios.Add(usuarioWrapper);
            }
            return listadoUsuarios;

        }


        static public List<KeyValuePair<MembershipUser, MembershipCreateStatus>> CreateUsers(List<UserDto> users, string password)
        {
            List<KeyValuePair<MembershipUser, MembershipCreateStatus>> resultUser = new List<KeyValuePair<MembershipUser, MembershipCreateStatus>>();

            foreach (UserDto user in users)
            { 
                MembershipCreateStatus result;

                MembershipUser newUser= Membership.CreateUser(user.UserName,password, user.Email,user.UserName,user.UserName,true,out result);

                resultUser.Add(new KeyValuePair<MembershipUser, MembershipCreateStatus>(newUser, result));

            }
            return resultUser;
        }



    }

    /// <summary>
    /// Dto for users
    /// </summary>
    [Serializable]
    public class UserDto
    {
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Result { get; set; }

    }

}